#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/6/3 上午10:49
# @Author  : nabby
# @File    : role_manage.py
# @Desc:

from infrastructure.po.impl.role_impl import RoleImpl
from infrastructure.po.impl.role_resource_impl import RoleResourceImpl
from infrastructure.po.impl.resource_impl import ResourceImpl
from infrastructure.utils.http import responser
from pprint import pprint, pformat


class RoleService(object):
    @classmethod
    def get_role_list(cls, name=None, page=1, page_size=999):
        """
        查询角色列表
        :param status: 状态
        :param name: 查询
        :param page: 页码
        :param page_size: 每页数量
        :return:
        """
        response_data = []
        role_list, total = RoleImpl.get_role_list(name=name, page=page, page_size=page_size)
        for role in role_list:
            response_data.append(
                {
                    "roleId": role.id,
                    "name": role.name,
                    "description": role.description,
                    "status": role.status,
                    "createTime": role.create_time,
                    "updateTime": role.update_time
                }
            )

        return responser.send(10000, data={"total": total, "data": response_data})

    @classmethod
    def add_role(cls, name, description):
        """
        新增角色
        :param status: 状态
        :param name: 角色名称
        :param description: 角色描述
        :return:
        """
        role_code = RoleImpl.add_role(
            name=name,
            description=description,
        )
        if role_code:
            return responser.send(10000)
        else:
            return responser.send(10006)

    @classmethod
    def update_role(
            cls,
            role_id,
            name,
            description
    ):
        """
        修改角色信息
        :param role_id: 角色id
        :param name: 角色名称
        :param description: 角色描述
        :return:
        """
        role_code = RoleImpl.update_role(
            role_id=role_id,
            name=name,
            description=description
        )
        if role_code:
            return responser.send(10000)
        else:
            return responser.send(10006)

    @classmethod
    def change_role_state(cls, role_id, state):
        """
        变更角色状态
        :param role_id: 角色id
        :param state: 状态
        :return:
        """
        RoleImpl.change_role_state(role_id=role_id, state=state)
        return responser.send(10000)

    @classmethod
    def delete_role(cls, role_id):
        """
        删除角色
        :param role_id: 角色id
        :return:
        """
        role_code = RoleImpl.delete_role(role_id=role_id)
        if role_code:
            return responser.send(10000)
        else:
            return responser.send(10008)

    # @classmethod
    # def get_menu_tree(cls):
    #     """
    #     获取菜单树
    #     :return:
    #     """
    #     menu_tree = RoleImpl.get_menu_tree()
    #     return responser.send(10000, data=menu_tree)

    @classmethod
    def get_menu_tree(cls):
        """
        获取菜单树
        :return:
        """
        first_menu, second_menu = RoleImpl.get_menu_tree()
        menu_dict = {}
        for fmenu in first_menu:
            menu_dict[fmenu.id] = {
                "menuCode": fmenu.perms,
                "menuName": fmenu.name,
                "menuOrder": fmenu.seq,
                "children": []
            }

        for smenu in second_menu:
            if smenu.sys_resource_id in menu_dict:
                menu_dict[smenu.sys_resource_id]["children"].append({
                    "menuCode": smenu.perms,
                    "menuName": smenu.name,
                    "menuOrder": smenu.seq,
                })
        print(list(menu_dict.values()))
        return responser.send(10000, data=list(menu_dict.values()))

    @classmethod
    def set_role_menu(cls, role_id, menu_tree):
        """
        设置角色菜单
        :param role_id: 角色id
        :param menu_tree: 菜单树
        :return:
        """
        menu_list = ResourceImpl.get_resource_list()
        menu_dict = {_resource.perms: _resource.id for _resource in menu_list}
        pprint({"menu_dict": menu_dict})

        for first_menu in menu_tree:
            pprint({"first_menu": first_menu})
            RoleResourceImpl.add_role_resource(role_id=role_id, resource_id=menu_dict[first_menu["menuCode"]])
            for second_menu in first_menu.get("children", []):
                pprint({"second_menu": second_menu})
                RoleResourceImpl.add_role_resource(role_id=role_id, resource_id=menu_dict[second_menu["menuCode"]])
        return responser.send(10000)